home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 1999 January - Disc 2 / Macworld (1999-01) (Disk 2).dmg / Serious Demos / Symbolic Composer 4.2 / Environment / System / SYMBOL / Symbol Generators / L-System / gen-lsystem < prev    next >
Lisp/Scheme  |  1998-10-23  |  3KB  |  123 lines

  1. gen-lsystem axiom depth valid-symbols &opt :tree
  2.  
  3. Rewrites axiom at the depth depth, transforms it into a symbol, and preserves only valid-symbols. If gen-lsystem is activated without valid-symbols, the rewritings are transformed as they are. Rewrite rules are defined with defsym function. L-system provides an elegant way to generate classical fractals like von Koch snowflakes, dragon curves and Peano and Hilbert space filling curves, that model the growth of living organisms.
  4.  
  5. (initdef)
  6. (defsym g '(g f x < + g > < - g >))
  7. (defsym x '(x < - f f f > < + f f f > f x))
  8. (defsym - '-)
  9. (defsym + '+)
  10. (defsym < '<)
  11. (defsym > '>)
  12.  
  13. (gen-lsystem g 2 '(f + - < >))
  14. --> (f e e e g g g f g e)
  15.  
  16. Or using a :tree.
  17.  
  18. (initdef 'another-tree)
  19. (defsym g '(g f x < - g > < + g >) :tree 'another-tree)
  20. (defsym x '(x < + f f f > < - f f f > f x) :tree 'another-tree)
  21. (defsym - '- :tree 'another-tree)
  22. (defsym + '+ :tree 'another-tree)
  23. (defsym < '< :tree 'another-tree)
  24. (defsym > '> :tree 'another-tree)
  25.  
  26. (gen-lsystem g 2 '(f + - < >) 'another-tree)
  27. --> (f g g g e e e f e g)
  28.  
  29. (gen-lsystem g 2 '(f + - < >))
  30. --> (f e e e g g g f g e)
  31.  
  32. To use L-system to build melodies, you can use directly the output, or rescale it to a new range.
  33.  
  34. (setq melody (symbol-scale '(a k) 
  35.                            (gen-lsystem g 2 '(f + - < >))))
  36.  
  37. To control velocities, use
  38.  
  39. (setq velocity-pattern
  40.     (vector-smooth 3 
  41.        (symbol-to-vector 
  42.            (gen-lsystem g 2 '(f + - < >)))))
  43.  
  44. To build chords use symbols-to-tonality. For rhythms use combine symbol-to-vector and vector-scale.
  45.  
  46. def-grammar equivalence:
  47.  
  48. (def-grammar 'example-above
  49.    g (g f x < - g > < + g >)
  50.    x (x < + f f f > < - f f f > f x)
  51.    - '-
  52.    + '+
  53.    < '< 
  54.    > '>
  55. )
  56.  
  57. Examples
  58.  
  59. ; koch-curve
  60.  
  61. (def-grammar 'koch-curve
  62.    axiom (f + f + f + f)
  63.    f (f + f - f - f f + f + f - f)
  64.    - (-)
  65.    + (+)
  66.    < (<)
  67.    > (>)
  68. )
  69.  
  70. (gen-lsystem axiom 3 '(f + - < >) 'koch-curve)
  71.  
  72. ; hilbert curve
  73.  
  74. (def-grammar 'hilbert-curve
  75.    axiom (x)
  76.    x (- y f + x f x + f y -)
  77.    y (+ x f - y f y - f x +)
  78.    - (-)
  79.    + (+)
  80.    < (<)
  81.    > (>)
  82. )
  83.  
  84. (gen-lsystem axiom 5 '(f + - < >) 'hilbert-curve)
  85.  
  86. ; peano curve
  87.  
  88. (def-grammar 'peano-curve
  89.    axiom (x)
  90.    x (x f y f x + f + y f x f y - f - x f y f x)
  91.    y (y f x f y - f - x f y f x + f + y f x f y)
  92.    - (-)
  93.    + (+)
  94.    < (<)
  95.    > (>)
  96. )
  97.  
  98. (gen-lsystem axiom 3 '(f + - < >) 'peano-curve)
  99.  
  100. ; dragon curve
  101.  
  102. (def-grammar 'dragon-curve
  103.    axiom (f x)
  104.    x (x + y f)
  105.    y (- f x - y)
  106.    - (-)
  107.    + (+)
  108.    < (<)
  109.    > (>)
  110. )
  111.  
  112. (gen-lsystem axiom 7 '(f + - < >) 'dragon-curve)
  113.  
  114. Dragon does not produce anything useful because the 2D rules happens to map here so that it all makes the curve going down.
  115.  
  116. One idea is to make the production rules randomly using defsym, for example
  117.  
  118. (defsym f (generate-random-production-rules) :tree 'random-rules)
  119. ...
  120.  
  121. (gen-lsystem axiom 3 '(f + - < >) 'random-rules)
  122.  
  123.